<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancel">
    <el-checkbox-group v-model="checkList">
      <!-- 多选框 -->
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-row justify="center" type="flex">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>

      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetail } from '@/api/user'
import { assignRole } from '@/api/employees'

export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    employeeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [], // 已勾选角色
      list: [] // 全部角色

    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, size: 999 })
      this.list = rows
    },
    async getUserDetail(id) {
      console.log(id)
      const res = await getUserDetail(id)
      console.log(res)
      this.checkList = [...res.roleIds]
    },
    // 确定 发请求
    async  btnOK() {
      // 给员工分配角色
      await assignRole({
        id: this.employeeId,
        roleIds: this.checkList
      })
      this.$message.success('操作成功')
      this.$emit('update:showRoleDialog', false)
    },
    // 取消
    btnCancel() {
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>

</style>
